import { defineConfig, loadEnv } from 'vite'
import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import vue from '@vitejs/plugin-vue'
// import AutoImport from 'unplugin-auto-import/vite'
// import Components from 'unplugin-vue-components/vite'
// import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
//引入svg需要用到插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// mock插件引入方法
import { viteMockServe } from 'vite-plugin-mock'



// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
  //获取各种环境下的对应的变量
  let env = loadEnv(mode, process.cwd());

  console.log(env, '环境是啥');

  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      // viteMockServe({
      //   localEnabled: command === 'serve',//保证开发阶段可以使用mock接口
      // }),
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src") // 相对路径别名配置，使用 @ 代替 src
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          // additionalData: '@import "./src/styles/variable.scss";',
          additionalData: `@use "@/styles/variable.scss" as *; @use "@/styles/reset.scss" as *;`,
        },
      },
    },

    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          //获取数据的服务器地址设置
          target: env.VITE_SERVE,
          //需要代理跨域
          changeOrigin: true,
          //路径重写
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
        // ['/prod-api']: {
        //   //获取数据的服务器地址设置
        //   target: env.VITE_SERVE,
        //   //需要代理跨域
        //   changeOrigin: true,
        //   //路径重写
        //   rewrite: (path) => path.replace(/^\/api/, ''),
        // },

      }
    }
  }
})
